//获取应用实例
const app = getApp()
Page({
  data: {
    id:'',
    name: '',//收货人姓名
    phone: '',//收货人电话
    default:true,
    positionDetails:'',//详情地址
    index: 0,
    province_list:null,
    province_name:null,
    city_list:null,
    city_name:null,
    area_list:null,
    area_name:null,
    addressCity:null,
    multiArray: [null,null,null],  // 三维数组数据
    multiIndex: [0, 0, 0], // 默认的下标,
    selectProvinceId:null,
    selectCityId:null,
    selectAreaId:null,
    // 组件所需的参数
    nvabarData: {
      showCapsule: 1, //是否显示左上角图标   1表示显示    0表示不显示
      title: '添加收货地址', //导航栏 中间的标题

    },
    // 此页面 页面内容距最顶部的距离
    height: app.globalData.height * 2 + 20,
  },
  // 加载时获取
  onLoad(options){
    if(options.id){
      let that = this;
      let id = options.id
      let url = app.globalData.URL+'goods/address/list?id='+id;
          let data = {};
          app.wxRequest('GET', url, data, (res) => {
            let livs = res.rows;
            let name = livs[0].name;
            let phone = livs[0].phone;
            let positionDetails = livs[0].positionDetails;
            let positionProvinceValue = livs[0].positionProvinceValue;
            let positionAreaValue = livs[0].positionAreaValue;
            let positionCityValue = livs[0].positionCityValue;
            that.setData({
              name:name,
              phone:phone,
              positionDetails:positionDetails,
              addressCity:[positionProvinceValue,positionAreaValue,positionCityValue],
            })
            console.log(livs)
          }, (err) => {
            console.log(err.errMsg)
          })
    }
    this.getProvince()
  },
  // 获取名字
  bindNameChange:function(e){
    this.setData({
      name:e.detail.value
    })
  },
  // 获取详细地址
  bindPositionDetailsChange:function(e){
    this.setData({
      positionDetails:e.detail.value
    })
  },
  // 获取电话号码
  bindPhoneChange:function(e){
    this.setData({
      phone:e.detail.value
    })
  },


  //获取省份列表
  getProvince:function(){
    let that = this;
    let url = app.globalData.URL+'goods/area/list';
        let data = {};
        app.wxRequest('GET', url, data, (res) => {
          let provinceList = [...res.rows]
          let provinceArr = res.rows.map((item)=>{return item.criName})  //获取名称
          that.setData({
            multiArray:[provinceArr,[],[]], //更新三维数组  更新完为[['广东','北京'],[],[]]
            province_list:provinceList,  //省级原始数据
            province_name:provinceArr,  //省级全部名称
          })
          let defaultCode = that.data.province_list[0].criCode //使用第一项当作参数获取市级数据
          if(defaultCode){
            that.setData({
              currnetProvinceId:defaultCode  //保存当前省份id
            })
          }
          that.getCity(defaultCode) //获取市区数据
        }, (err) => {
          console.log(err.errMsg)
        })
  },
  //获取城市列表
  getCity:function(e){
    let that = this;
    let code = e;
    let url = app.globalData.URL+'goods/area/list?code='+code;
        let data = {};
        app.wxRequest('GET', url, data, (res) => {
          let cityArr = res.rows.map((item)=>{return item.criName}) //返回城市名称
          let cityList = [...res.rows]
          
          that.setData({
            multiArray:[that.data.province_name,cityArr,[]],  //更新后[['广东','北京'],['潮州'，'汕头','揭阳'],[]]
          city_list:cityList, //保持市级数据
          city_name:cityArr   //市级名称
          })
          let defaultCode = that.data.city_list[0].criCode //使用第一项当作参数获取市级数据
          if(defaultCode){
            that.setData({
              currentCityId:defaultCode  //保存当前省份id
            })
          }
          that.getArea(defaultCode) //获取区域数据
        }, (err) => {
          console.log(err.errMsg)
        })
  },
  //获取区域列表
  getArea:function(e){
    let that = this;
    let code = e;
    let url = app.globalData.URL+'goods/area/list?code='+code;
        let data = {};
        app.wxRequest('GET', url, data, (res) => {
          let areaArr  = res.rows.map((item)=>{return item.criName}) //返回区域名
          let areaList  = [...res.rows]
          
          that.setData({
          multiArray: [that.data.province_name, that.data.city_name, areaArr],
            area_list: areaList, //区列表
            area_name: areaArr   //区名字
          })
          let defaultCode = that.data.city_list[0].criCode //使用第一项当作参数获取市级数据
          if(defaultCode){
            that.setData({
              currentCityId:defaultCode  //保存当前省份id
            })
          }
        }, (err) => {
          console.log(err.errMsg)
        })
  },
  //获取省市区名回显传code
  bindRegionChange:function(e){
    // 因为在获取省中 北京只有一个选项，导致获取不了北京》北京》第一个
    console.log(e)
    let that = this;
    if(e.detail.value[1]==null || e.detail.value[2]==null){ //如果只滚动了第一列则选取第一列的第一数据
      that.setData({
        multiIndex: e.detail.value,  //更新下标
        addressCity: [that.data.province_list[e.detail.value[0]].criName, that.data.city_list[0].criName, that.data.area_list[0].criName],
        selectProvinceId: that.data.province_list[e.detail.value[0]].criCode,
        selectCityId: that.data.city_list[0].criCode,
        selectAreaId: that.data.area_list[0].criCode
      })
      console.log("城市名字：",that.data.province_list[e.detail.value[0]].criName, that.data.city_list[0].criName, that.data.area_list[0].criName)
    }else{
      that.setData({
        multiIndex: e.detail.value,  //更新下标
        addressCity: [that.data.province_list[e.detail.value[0]].criName, that.data.city_list[e.detail.value[1]].criName, that.data.area_list[e.detail.value[2]].criName],
        selectProvinceId: that.data.province_list[e.detail.value[0]].criCode,
        selectCityId: that.data.city_list[e.detail.value[1]].criCode,
        selectAreaId: that.data.area_list[e.detail.value[2]].criCode
      })
    }
    console.log("城市名字：",that.data.province_list[e.detail.value[0]].criName, that.data.city_list[e.detail.value[1]].criName, that.data.area_list[e.detail.value[2]].criName)
    console.log("地区id：",that.data.province_list[e.detail.value[0]].criCode,that.data.city_list[e.detail.value[1]].criCode,that.data.area_list[e.detail.value[2]].criCode)
    console.log('地区id',that.data.selectProvinceId,that.data.selectCityId,that.data.selectAreaId,)
  },
  //滑动地区组件
  bindRegionColumnChange:function(e){
    console.log(e.detail.column,e.detail.value)
    let that = this
    let column = e.detail.column  //当前改变的列
    let data = {
      multiIndex:JSON.parse(JSON.stringify(that.data.multiIndex)),
      multiArray:JSON.parse(JSON.stringify(that.data.multiArray))
    }
    data.multiIndex[column] = e.detail.value  //第几列改变了就是对应multiIndex的第几个，更新他
    switch(column){
      case 0 :  //第一列改变，省级改变
        let currentProvinceId = that.data.province_list[e.detail.value].criCode
        if(currentProvinceId != that.data.currentProvinceId){ //判断当前id是不是更新了
          that.getCity(currentProvinceId)   //获取当前id下的市级数据
        }
        data.multiIndex[1] = 0    //将市默认选择第一个
        break
      case 1 :  //第二列改变，市级改变
        let currentCityId = that.data.city_list[e.detail.value].criCode
        if(currentCityId != that.data.currentCityId){
          that.getArea(currentCityId) //获取区域
        }
        data.multiIndex[2] = 0  //区域默认第一个
        break
    }
    that.setData(data)
  },

  // 提交
  submit_live:function(){
    let that = this;
    console.log('地区id',that.data.selectProvinceId,that.data.selectCityId,that.data.selectAreaId,)
    wx.showLoading({
      title: '提交中',
    })
    let  name= that.data.name;//收货人姓名
    let  phone= that.data.phone;//收货人姓名
    let  positionDetails= that.data.positionDetails;//收货人详细地址
    // console.log(name,phone,positionProvince,positionArea,positionCity,positionDetails)
     
      let url = app.globalData.URL+'goods/address?name='+that.data.name+'&phone='+that.data.phone+'&positionDetails='+that.data.positionDetails+'&positionProvince='+that.data.selectProvinceId+'&positionCity='+that.data.selectAreaId+'&positionArea='+that.data.selectCityId;
        let data = {
            // name:name,
            // phone:phone,
            // positionDetails:positionDetails,
            // positionProvince:positionProvince,
            // positionCity:positionCity,
            // positionArea:positionArea
        };
        app.wxRequest('POST', url, data, (res) => {
          wx.hideLoading();
          if(res.code!=200){
            wx.showModal({
              title: res.msg,
            })
          }else{
            wx.showModal({
              title: '添加地址成功',
              success(){
                wx.navigateBack({
                  delta:2
                })
              }
            })
          }
          console.log(res)
        }, (err) => {
          console.log(err.errMsg)
        }) 
      }
})
